﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

using System.Reflection;
using RibbonEditor.CommandUIDefinitionClasses;
using RibbonEditor.Forms;

namespace RibbonEditor.AppCode
{
    public class ContextMenuStripHelper
    {
        public static ContextMenuStrip BuildUICommandDefContextMenuStrip(TreeNode relatedNode, string[] commandsAddition, bool addDeleteOption)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.ItemClicked += new ToolStripItemClickedEventHandler(cms_UICommandDefItemClicked);
            cms.Tag = relatedNode;

            if (commandsAddition != null)
            {
                foreach (string s in commandsAddition)
                {
                    ToolStripMenuItem tsmi = new ToolStripMenuItem("Add " + s);
                    cms.Items.Add(tsmi);
                }

                ToolStripSeparator tss2 = new ToolStripSeparator();
                cms.Items.Add(tss2);
            }
            
            ToolStripMenuItem tsmiDx = new ToolStripMenuItem("Display Xml");
            cms.Items.Add(tsmiDx);

            if (addDeleteOption)
            {
                if (cms.Items.Count > 0)
                {
                    ToolStripSeparator tss = new ToolStripSeparator();
                    cms.Items.Add(tss);
                }

                ToolStripMenuItem tsmi = new ToolStripMenuItem("Delete");
                cms.Items.Add(tsmi);
            }

            return cms;
        }

        static void cms_UICommandDefItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            TreeNode selectedNode = (TreeNode)e.ClickedItem.GetCurrentParent().Tag;

            if (e.ClickedItem.Text == "Delete")
            {
                selectedNode.TreeView.SelectedNode = selectedNode.Parent;

                UIActionHelper.Delete(selectedNode);
                //selectedNode.Parent.Nodes.Remove(selectedNode);
            }
            else if (e.ClickedItem.Text == "Display Xml")
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml("<root></root>");

                ((IXmlBuildable)selectedNode.Tag).AddNode(doc.DocumentElement);

                XmlContentDisplayDialog dialog = new XmlContentDisplayDialog(doc.DocumentElement.InnerXml);
                dialog.StartPosition = FormStartPosition.CenterParent;
                dialog.ShowDialog();
            }
            else
            {
                // Rebuild class name
                string itemText = e.ClickedItem.Text;
                string className = itemText.Remove(0, 4).Replace(" ", "");

                Assembly assembly = Assembly.GetExecutingAssembly();
                AssemblyName assemblyName = assembly.GetName();

                Type t = Type.GetType(assemblyName.Name + ".CommandUIDefinitionClasses." + className);
                
                ICommandUIDefinition cuid;
                
                if (className != "Controls")
                {
                    cuid = (ICommandUIDefinition)Activator.CreateInstance(t);
                }
                else
                {
                    bool isFromCommandUIDef = e.ClickedItem.Text == "CommandUIDefinition";

                    cuid = (ICommandUIDefinition)Activator.CreateInstance(t, new object[] { isFromCommandUIDef });
                }

                if (selectedNode.Tag is IParentable)
                {
                    ((IParentable)selectedNode.Tag).AddChild(cuid);
                }

                cuid.BuildTreeViewElement(selectedNode);

                selectedNode.Expand();
            }
        }

        public static void TreeViewRightClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                switch (e.Node.Tag.GetType().ToString())
                {
                    case "RibbonEditor.CommandUIDefinitionClasses.Unit":
                        {
                            TreeNode parentNode = e.Node.Parent;

                            if (((Spinner)parentNode.Tag).Units.Count == 1)
                            {
                                e.Node.ContextMenuStrip.Items[2].Enabled = false;
                            }
                        }
                        break;
                }
            }
        }

        public static ContextMenuStrip BuildStandardContextMenuStrip(TreeNode relatedNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Tag = relatedNode;

            AddDisplayXmlMenu(cms, false);
            AddDeleteMenu(cms, true);

            return cms;
        }

        static void cms_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            //TreeNode selectedNode = (TreeNode)e.ClickedItem.GetCurrentParent().Tag;

            //switch (e.ClickedItem.Text)
            //{
            //    case "Display Xml":
            //        {
            //            XmlDocument doc = new XmlDocument();
            //            doc.LoadXml("<root></root>");

            //            ((IXmlBuildable)selectedNode.Tag).AddNode(doc.DocumentElement);

            //            XmlContentDisplayDialog dialog = new XmlContentDisplayDialog(doc.DocumentElement.InnerXml);
            //            dialog.StartPosition = FormStartPosition.CenterParent;
            //            dialog.ShowDialog();
            //        }
            //        break;
            //    case "Delete":
            //        {
            //            selectedNode.TreeView.SelectedNode = selectedNode.Parent;
            //            UIActionHelper.Delete(selectedNode);
            //            //selectedNode.Parent.Nodes.Remove(selectedNode);

            //        }
            //        break;
            //}
        }

        #region Display Xml Handler

        public static void AddDisplayXmlMenu(ContextMenuStrip cms, bool addPreSeparator)
        {
            if (addPreSeparator)
            {
                ToolStripSeparator separator = new ToolStripSeparator();
                cms.Items.Add(separator);
            }

            ToolStripMenuItem item = new ToolStripMenuItem("Display Xml");
            cms.Items.Add(item);
            item.Click += new EventHandler(displayXmlitem_Click);
        }

        static void displayXmlitem_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<root></root>");

            ((IXmlBuildable)selectedNode.Tag).AddNode(doc.DocumentElement);

            XmlContentDisplayDialog dialog = new XmlContentDisplayDialog(doc.DocumentElement.InnerXml);
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.ShowDialog();
        }

        #endregion

        #region Delete Handler

        public static void AddDeleteMenu(ContextMenuStrip cms, bool addPreSeparator)
        {
            if (addPreSeparator)
            {
                ToolStripSeparator separator = new ToolStripSeparator();
                cms.Items.Add(separator);
            }

            ToolStripMenuItem item = new ToolStripMenuItem("Delete");
            cms.Items.Add(item);
            item.Click += new EventHandler(deleteitem_Click);
        }

        static void deleteitem_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

            selectedNode.TreeView.SelectedNode = selectedNode.Parent;
            UIActionHelper.Delete(selectedNode);
        }

        #endregion
    }
}
